/* File:        	RequestSerializedImpl.java
* Package:			org.esi.authz.common.comm.request
* Created:      	29-abr-2011
* Last Changed: 	0:10:46
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.common.comm.request;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jboss.security.xacml.factories.RequestResponseContextFactory;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.sunxacml.ParsingException;
import org.jboss.security.xacml.sunxacml.ctx.RequestCtx;

import com.tecnalia.authz.common.XACMLConstants;
import com.tecnalia.authz.common.log.PDPLog;

// TODO: Auto-generated Javadoc
/**
 * The Class RequestSerializedImpl.
 */
public class RequestSerializedImpl implements Serializable, IRequestSerialized{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The requestlist. */
	   //	private static PDPLog logger = new PDPLog(RequestSerializedImpl.class);	

	/** The Request. */
	public List<String> requestlist = new ArrayList<String>();
   	
	/** The Query mode. */
	private int QueryMode = XACMLConstants.XACML_QUERY_NOT_STABLISHED;

	/* (non-Javadoc)
	 * @see org.esi.authz.common.comm.request.IRequestSerialized#getQueryMode()
	 */
	@Override
	public int getQueryMode() {
		return QueryMode;
	}

	/* (non-Javadoc)
	 * @see org.esi.authz.common.comm.request.IRequestSerialized#setQueryMode(int)
	 */
	@Override
	public void setQueryMode(int queryMode) {
		QueryMode = queryMode;
	}
   	
	/* (non-Javadoc)
	 * @see org.esi.authz.common.comm.request.IRequestSerialized#parseRequest()
	 */
   	@Override
	public RequestContext[] deserializeRequest(){

   		RequestContext[] requestContext = new RequestContext[this.requestlist.size()];  
		   int i = 0;
		   for (Iterator<String> iterator = this.requestlist.iterator(); iterator.hasNext();) {
			   
			   String reqPlain = iterator.next();
			   //Parse the inputStream to an XACML Object
			   RequestContext request = RequestResponseContextFactory.createRequestCtx();
			   InputStream is = null;
			   try {
					is = new ByteArrayInputStream(reqPlain.getBytes("UTF-8"));
			   } catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				RequestCtx req = null;
				
				try {
					req = RequestCtx.getInstance(is);
				} catch (ParsingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.set(org.jboss.security.xacml.interfaces.XACMLConstants.REQUEST_CTX, req);
				
				//Log the request
				
				ByteArrayOutputStream baos_p = new ByteArrayOutputStream();			
				try {
					request.marshall(baos_p);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if ( PDPLog.logger.isDebugEnabled() ){
					//logger.debug("SERVER Request - Parse : " + baos_p.toString());
				}
				
				requestContext[i] = request;
				i++;
				//req.encode(baos_p);				
				//this.Lpdp.info("SERVER Response - Parse : " + baos_p.toString());					
			
		}
		   

       	
			
			return requestContext;
	   }
   		
		   /* (non-Javadoc)
		    * @see org.esi.authz.common.comm.request.IRequestSerialized#serializeRequest(org.jboss.security.xacml.interfaces.RequestContext[])
		    */
		   @Override
   		public void serializeRequest(RequestContext[] request){
   	        for (int i = 0; i < request.length; i++) {
   	            ByteArrayOutputStream baos = new ByteArrayOutputStream();
   	            try {
					request[i].marshall(baos);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}      
   	      
   	            //Prepare the objetc to be serialize
   	            this.requestlist.add(baos.toString());
   	            /*if( PDPLog.logger.isDebugEnabled()){
   	                this.logger.debug("CLIENT REQUEST[" + baos.toString() + 
   	                					"] SEND : " + baos.toString());	            	
   	            }*/
   			}            

   		}
   	
   	
   		/* (non-Javadoc)
		 * @see org.esi.authz.common.comm.request.IRequestSerialized#LogRequest()
		 */
   		@Override
		public void LogRequest(){ 
			for (Iterator<String> iterator = this.requestlist.iterator(); iterator.hasNext();) {
				String reqPlainText = iterator.next();
				//logger.debug("SERVER Request : " + reqPlainText); 
			}
   		}
   		
   		
}
